layui.use(['form', 'layedit', 'laydate'], function(){
    var form = layui.form
        ,layer = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate;

    layuiFunciton.ajaxRequest({
        url:  layuiFunciton.URL + '/exportTableData/findTable',
        method: 'get',
        success: function(data){
            var html = '';
            for(var key in data){
                html+='<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;"><legend>'+key+'</legend></fieldset>';
                html+='<form class="layui-form" action="">';
                    html+='<div class="layui-form-item">';
                        html+='<div class="layui-input-block">';
                            $.each(data[key],function (index,obj) {
                                html+='<input class="like" value="'+obj+'" type="checkbox" checked="checked" name="like" title="'+obj+'">';
                            });
                        html+='</div>';
                    html+='</div>';
                html+='</form>';
            }
            $("#tabelId").html(html);

        }
    });
    //备份设置
    layuiFunciton.ajaxRequest({
        url:  layuiFunciton.URL + '/dataBackup/backupConfigure',
        method: 'get',
        success: function(data){
            $("#id").val(data.id);
            $("#"+data.backupTime).attr("selected","selected");
            if(data.states==3){
                $("#states").val(3);
                $("#states").attr("checked","checked");
                $(".like").attr("disabled","disabled");
                $("#time").show();
                $("a").html("设置");
                $("#a").hide();
            }else if(data.states==4){
                $("#states").val(4);
                $(".like").removeAttr("disabled");
                $("#time").hide();
                $("a").show();
                $("#a").html("备份")
            }
        }
    });
    //监听下拉列表
    form.on('select(backupTime)', function(data){
        layuiFunciton.ajaxRequest({
            url:  layuiFunciton.URL + '/dataBackup/update',
            method: 'post',
            data: {id:$("#id").val(),backupTime:$("#backupTime").val(),backupState:$("#backupState").val()},
            success: function(data){
                layuiFunciton.success("操作成功!",function () {
                    window.location.reload();
                })
            }
        })
    });

    //监听提交
    form.on('submit(demo1)', function(data){
        var list = $(".like");
        var strs = [];
        for (var i = 0; i < list.length; i++) {
            if(list.eq(i).attr("checked")=="checked")
                strs.push(list.eq(i).val());
        }
        var str = strs.join(",");
        layuiFunciton.ajaxRequest({
            url:  layuiFunciton.URL + '/exportTableData/exportTable',
            method: 'post',
            title:'数据备份中...',
            data: {tabelNames:str,states:123},
            success: function(data){
                layuiFunciton.success("备份完成",function () {
                    layuiFunciton.addTab({title:'备份历史',url:'/page/core/sysDataBackup/history.html'});
                })
            }
        });
        return false;
    });
    //监听指定开关
    form.on('switch(switchTest)', function(data){
        if(this.checked){
            $(this).val(3);
            $(".like").attr("checked","checked");
            $(".like").attr("disabled","disabled");
            form.render();
            $("#time").show();
            $("a").html("设置");
            $("#a").hide();
        }else{
            $(this).val(4);
            $(".like").removeAttr("disabled");
            form.render();
            $("#time").hide();
            $("a").html("备份");
            $("#a").show();
        }
    });


    //监听指定开关
    form.on('switch(backupState)', function(data){
        if(this.checked){
            $(this).val(1);
        }else{
            $(this).val(2);
        }
    });
});
